package com.incar.chuxing.utils.ble.utils;


import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * @author WangJintao
 * @date 2016-4-9 下午6:05:21 类说明
 */
public class StringUtils {
    private static List<byte[]> socketList = new ArrayList<byte[]>();

    private static List<byte[]> obdList = new ArrayList<byte[]>();

    /**
     * yyyy-MM-dd
     *
     * @return
     */
    public static String getCurrentYearMonthDate() {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return simpleDateFormat.format(date);
    }

    public static long toBeiJingDateTime(String format, String date) {
        Date millDate = new Date();
        long time = millDate.getTime();
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format, Locale.CHINA);
            // 必须捕获异常
            try {
                if (date != null && !date.equals("")) {
                    millDate = simpleDateFormat.parse(date);
                    time = millDate.getTime() + 8 * 3600 * 1000;
                }

            } catch (java.text.ParseException e) {
                e.printStackTrace();
            }
        } catch (NullPointerException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        }

        return time;
    }

    /**
     * yyyy-MM-dd
     *
     * @param date
     * @return
     */
    public static long getMillisecondsByDateDay(String date) {
        // 注意：SimpleDateFormat构造函数的样式与strDate的样式必须相符
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        // 必须捕获异常
        Date millDate = new Date();
        try {
            if (date != null && !date.equals(""))
                millDate = simpleDateFormat.parse(date);
        } catch (java.text.ParseException e) {
            e.printStackTrace();
        }
        return millDate.getTime();
    }

    public static String toStringHex1(String s) {
        byte[] baKeyword = new byte[s.length() / 2];
        for (int i = 0; i < baKeyword.length; i++) {
            try {
                baKeyword[i] = (byte) (0xff & Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        try {
            s = new String(baKeyword, "utf-8");// UTF-16le:Not
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return s;
    }

    /**
     * 数组的合并
     *
     * @param byte_1
     * @param byte_2
     * @return
     */
    public static byte[] byteMerger(byte[] byte_1, byte[] byte_2) {
        byte[] byte_3 = new byte[byte_1.length + byte_2.length];
        System.arraycopy(byte_1, 0, byte_3, 0, byte_1.length);
        System.arraycopy(byte_2, 0, byte_3, byte_1.length, byte_2.length);
        return byte_3;
    }

    /**
     * 从一个byte[]数组中截取一部分
     *
     * @param src
     * @param begin
     * @param count
     * @return
     */
    public static byte[] subBytes(byte[] src, int begin, int count) {
        byte[] bs = new byte[count];
        for (int i = begin; i < begin + count; i++)
            bs[i - begin] = src[i];
        return bs;
    }

    private final static Object lock1 = new Object();

    private final static Object lock2 = new Object();


    /**
     * 字符串是否为空
     *
     * @param string
     * @return
     */
    public static boolean isEmpty(String string) {
        if (null == string || "null".equalsIgnoreCase(string)) {
            return true;
        }
        if (0 == string.trim().length() || 0 == string.replace(" ", "").length()) {
            return true;
        }

        return false;
    }

    /**
     * byte数组中取int数值，本方法适用于(低位在前，高位在后)的顺序，和和intToBytes（）配套使用
     *
     * @param src    byte数组
     * @param offset 从数组的第offset位开始
     * @return int数值
     */
    public static int bytesToInt(byte[] src, int offset) {
        int value;
        value = (int) ((src[offset] & 0xFF) | ((src[offset + 1] & 0xFF) << 8)
                | ((src[offset + 2] & 0xFF) << 16) | ((src[offset + 3] & 0xFF) << 24));
        return value;
    }

    /**
     * byte数组中取int数值，本方法适用于(低位在后，高位在前)的顺序。和intToBytes2（）配套使用
     */
    public static int bytesToInt2(byte[] src, int offset) {
        int value;
        value = (int) (((src[offset] & 0xFF) << 24) | ((src[offset + 1] & 0xFF) << 16)
                | ((src[offset + 2] & 0xFF) << 8) | (src[offset + 3] & 0xFF));
        return value;
    }


    public static void logExceptions(Exception exception) {
        Writer info = new StringWriter();
        PrintWriter printWriter = new PrintWriter(info);
        exception.printStackTrace(printWriter);
        printWriter.close();
        // System.out.println("---------------exception:"+info.toString());
    }

    /**
     * 判断字符串是否是浮点数
     */
    public static boolean canBeParseDouble(String value) {
        try {
            Double.parseDouble(value);

        } catch (NumberFormatException e) {
            return false;
        }
        return true;
    }

    /**
     * 是否需要加密
     *
     * @param httpUrl
     * @return
     */
    public static boolean isNeedEncode(String httpUrl) {
        boolean result = false;
        if (!StringUtils.isEmpty(httpUrl)) {
            result = !httpUrl.contains(String.valueOf("CloudVerificationService"))
                    && !httpUrl.contains(String.valueOf("CheckBoundDevice"))
                    && !httpUrl.contains(String.valueOf("BoundDevice1"))
                    && !httpUrl.contains(String.valueOf("https://"))
                    && !httpUrl.contains(String.valueOf("baoyang"))
                    && !httpUrl.contains(String.valueOf("getbaoyang"))
                    && !httpUrl.contains(String.valueOf("getSMSVerificationCode"));
        }
        return result;
    }

}
